import { createStore } from 'vuex'

export default createStore({
  state: {
    isShowMainTabBar: true,
    cartList: []
  },
  mutations: {
    showMainTabBar(state, data) {
      state.isShowMainTabBar = data
    },
    addToCart(state,data){
      state.cartList.push(data)
    },
    addCount(state,data) {
      data.count+=1
    }
  },
  actions: {
    addCart(context, data) {
      return new Promise((resolve,reject)=>{
        let oldData = null
        oldData = context.state.cartList.find((item) => {    //把重复对象的地址赋值给oldData
          return item.iid === data.iid
        })
        if (oldData) {
          context.commit('addCount',oldData)
          resolve('商品数量加一')
        } else {
          data.count = 1
          context.commit('addToCart',data)
          resolve('商品成功加入购物车')
        }
      })
    }
  },
  modules: {
  }
})
